subscribed.mediator

  • Declaration

    struct Mediator(params...);

    A simple mediator implementation. More precisely, an event collection with a unified interface and beforeEach/afterEach hooks.

    Parameters

    Examples

    The mediator events can be strings.

    1. Mediator!( "start", void delegate(), "stop", void delegate() ) mediator; mediator.emit!"start"();

    Examples

    The mediator events can be enum members.

    1. enum Event { start, stop } Mediator!( Event.start, void delegate(), Event.stop, void delegate() ) mediator; mediator.emit!(Event.start)();

    Examples

    The mediator events cannot be of mixed type.

    1. immutable canCompile = __traits(compiles, Mediator!( "start", void delegate(), 3, void delegate() )); assert(!canCompile, "Can compile mediators with mixed index types");

    Examples

    The mediator can subscribe, unsubscribe and broadcast events.

    1. Mediator!( "inc", void delegate(), "dec", void delegate(), "reset counter", void delegate() ) mediator; int counter; void increment() { counter++; } void decrement() { counter--; } void reset() { counter = 0; } mediator.on!"inc"(&increment); mediator.on!"dec"(&decrement); mediator.on!"reset counter"(&reset); assert(counter == 0, "Mediator functions are called before any action is performed"); mediator.emit!"inc"(); assert(counter == 1, "The mediator does not call one of it's functions"); mediator.emit!"dec"(); assert(counter == 0, "The mediator does not call one of it's functions"); assert(counter == 0, "Mediator functions are called before any action is performed"); mediator.emit!"inc"(); assert(counter == 1, "The mediator does not call one of it's functions"); mediator.emit!"reset counter"(); assert(counter == 0, "The mediator does not call one of it's functions"); mediator.beforeEach ~= string => false; assert(counter == 0, "The beforeEach hook does not work"); mediator.emit!"inc"(); assert(counter == 0, "The beforeEach hook does not work"); mediator.emit!"dec"(); assert(counter == 0, "The beforeEach hook does not work"); mediator.beforeEach.clear(); mediator.off!"inc"(&increment); mediator.off!"dec"(&decrement); assert(counter == 0, "The mediator called one of it's functions while unregistering them"); mediator.emit!"inc"(); assert(counter == 0, "The mediator did not remove a listener"); mediator.emit!"dec"(); assert(counter == 0, "The mediator did not remove a listener");

    • Declaration

      alias IType = typeof(params[0]);

      The type of the (indexing) channel names.

    • Declaration

      Event!(bool delegate(IType)) beforeEach;

      The hook to be executed before any transition. If false is returned, the no transition occurs.

    • Declaration

      Event!(void delegate(IType)) afterEach;

      The hook to be executed after a successful transition.

    • on

      Declaration

      void on(IType channel)(EventType.ListenerType[] listeners...);

      A function for appending listeners to the channel event.

      Parameters

      channel

      The channel whose event to subscribe to.

      EventType.ListenerType[] listeners

      The listeners to append.

      See Also

      subscribed.event.Event.append

    • off

      Declaration

      void off(IType channel)(EventType.ListenerType[] listeners...);

      A function for removing listeners from the channel event.

      Parameters

      channel

      The channel whose event to remove from.

      EventType.ListenerType[] listeners

      The listeners to remove.

      See Also

      subscribed.event.Event.append

    • Declaration

      void emit(string channel)(EventType.ParamTypes params);

      Calls all the registered listeners for the channel in order.

      Parameters

      channel

      The channel to emit a message to.

      EventType.ParamTypes params

      The param tuple to call the listeners with.

      Return Value

      An array of results from the listeners. If EventType.ReturnType is void, then this function also returns void.

      See Also

      subscribed.event.Event.call